/**
 * @param {Object} param
 */
module.exports = async function(param) {
	const clientInfo = this.clientInfo
	const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
		clientInfo
	})
	dbJQL.setUser({
		// uid:user.uid,
		role: ['admin']
	})

	let resOrganization = await dbJQL.collection('tian-organization').where(
		`original== '${param.organization}'`).field('original').get()
	let resOrganData = resOrganization.data
	let Organization_id
	if (resOrganData.length === 0) {
	  // 如果没有找到对应的记录，则插入新数据并获取其 ID
	  let res = await dbJQL.collection('tian-organization').add({
	    original: param.organization,
	    lang: param.lang
	  });
	
	  // 获取插入记录的 ID
	  Organization_id = res.id; // `add` 方法返回的数据中有 `id` 字段
	} else {
	  // 如果已找到数据，则直接使用现有记录的 ID
	  Organization_id = resOrganData[0]._id;
	}

	// return {
	// 	Organization_id
	// }


	let {
		name,
		phone_code,
		email,
		gender,
		lang,
		// organization,
		phone,
		position,
		organization_id
	} = param
	
	try {
		let res = await dbJQL.collection('uni-id-users').doc(param._id).update({
			
			personal_information: {
				name,
				phone_code,
				email,
				gender,
				lang,
				phone,
				position,
				organization_id:[...new Set([Organization_id,...organization_id])],
			}
		})

		return {
			errCode: 0,
			errMsg: 'success',
			data: res
		}
	} catch (error) {
		return {
			errCode: 0,
			errMsg: 'no-success',
		}
	}

}